package com.karson.front.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.karson.common.constants.Constants;
import com.karson.common.request.AlbumSearchRequest;
import com.karson.common.request.CommentRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommentResponse;
import com.karson.common.response.CommonResult;
import com.karson.common.response.RepliesResponse;
import com.karson.service.service.AlbumCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/comment")
@Tag(name = "评论管理接口")
public class CommentController {
    @Autowired
    private AlbumCommentService albumCommentService;

    @Operation(summary = "分页列表")
    @GetMapping("/list/{albumId}")
    public CommonResult<RepliesResponse> getList(@RequestParam(required = false) String offset, @PathVariable Integer albumId, @RequestParam(required = false) Integer orderType) throws JsonProcessingException {
        if (albumId == null) {
            return CommonResult.failed();
        }
        RepliesResponse repliesResponse = albumCommentService.pageList(offset, albumId, orderType);
        return CommonResult.success(repliesResponse);
    }

    @Operation(summary = "获取子评论分页列表")
    @GetMapping("/getSubList/{albumId}")
    public CommonResult<IPage<CommentResponse>> getSubList(@PathVariable Integer albumId, @RequestParam String rId, @RequestParam(required = false) Integer orderType, @RequestParam Integer current) {
        if (rId == null) {
            return CommonResult.failed();
        }
        IPage<CommentResponse> page = new Page<>(current, Constants.PAGE_SMALL_SIZE);
        IPage<CommentResponse> pageList = albumCommentService.getPageList(page,albumId,orderType,rId);
        return CommonResult.success(pageList);
    }
    @Operation(summary = "添加评论")
    @PostMapping("/add")
    public CommonResult<CommentResponse> addComment(@RequestBody CommentRequest request) throws JsonProcessingException {
        CommentResponse commentResponse = albumCommentService.addComment(request);
        return CommonResult.success(commentResponse);
    }
    @Operation(summary = "删除评论")
    @DeleteMapping("/del/{id}")
    public CommonResult<CommentResponse> delComment(@PathVariable Integer id){
        if (id == null || id <= 0) {
            return CommonResult.validateFailed();
        }
        Boolean flag = albumCommentService.delComment(id);
        return flag ? CommonResult.success("ok") : CommonResult.failed();
    }

}
